<script setup name="notice">
import { getNoticeList, createNotice, updateNotice, deleteNotice } from '@/api/notice'
import { useInitTable, useInitForm } from '@/composables/useCommon'
import FormDialog from '@/components/FormDialog.vue'
import ListHeader from '@/components/ListHeader.vue'

const { windowHeight, tableData, loading, currentPage, total, limit, getData, handleDelete } = useInitTable({
  getList: getNoticeList,
  delete: deleteNotice,
})

// 解构表单公共方法
const {
  form,
  dialogRef,
  formRef,
  rules,
  dialogTitle,
  handleSubmit,
  handleCreate,
  handleEdit,
  resetForm,
} = useInitForm({
  getData,
  updata: updateNotice,
  create: createNotice,
  form: {
    title: '',
    content: ''
  },
  rules: {
    title: [{ required: true, message: '公告标题不能为空', trigger: 'blur' }],
    content: [{ required: true, message: '公告内容不能为空', trigger: 'blur' }],
  },
})

// 减去头部高度、标签高度、内边距高度
let h = windowHeight.value - 64 - 44 - 36
const h2 = windowHeight.value - 64 - 44 - 100
</script>

<template>
  <el-card shadow="never" :body-style="{ 'padding-bottom': '10px', 'padding-left': '0', 'padding-right': '0' }" :style="{ height: `${h}px` }">
    <ListHeader create="新增公告" @create="handleCreate" @refresh="getData" />
    <div class="card-table" :style="{ height: `${h2}px` }">
      <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
        <el-table-column prop="title" label="公告标题" />
        <el-table-column prop="create_time" label="发布时间" min-width="100" align="center" />
        <el-table-column label="操作" width="116" align="center">
          <template #default="{ row }">
            <el-tooltip effect="light" content="修改" placement="left">
              <el-button type="primary" size="small" text @click="handleEdit(row)">
                <el-icon><View /></el-icon>
              </el-button>
            </el-tooltip>
            <el-popconfirm width="180" title="是否要删除该公告？" confirmButtonText="确认" cancelButtonText="取消" @confirm="handleDelete(row.id)">
              <template #reference>
                <el-button text type="primary" size="small" style="margin-left: 0">
                  <el-icon><CloseBold /></el-icon>
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="暂无数据" :image-size="200" />
        </template>
      </el-table>

      <div class="notice-pagination">
        <el-pagination background small layout="prev, pager, next" :total="total" :current-page="currentPage" :page-size="limit" :hide-on-single-page="total < 11" @current-change="getData" />
      </div>
    </div>

    <FormDialog ref="dialogRef" :width="60" :title="dialogTitle" @submit="handleSubmit" @close="resetForm">
      <el-form :model="form" ref="formRef" :rules="rules" :inline="false">
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入公告标题" />
        </el-form-item>
        <el-form-item label="公告内容" prop="content">
          <el-input v-model="form.content" placeholder="请输入公告内容" type="textarea" :rows="5" />
        </el-form-item>
      </el-form>
    </FormDialog>
  </el-card>
</template>

<style lang="scss" scoped>
.el-card {
  border: 0;
  overflow-y: auto;
  .notice-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    padding: 0 20px;
    .el-icon:first-child {
      color: var(--white);
    }
  }
  .card-table {
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    .el-table {
      z-index: 0;
    }
    .notice-pagination {
      display: flex;
      justify-content: center;
      padding-top: 10px;
    }
  }
}
@media screen and (max-width: 375px), screen and (max-width: 414px) {
  :deep(.el-dialog--center) {
    min-width: 70% !important;
    max-width: 70% !important;
  }
}
</style>
